﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCPets.Helpers
{
  public static class DictionaryHelper
  {
    #region Public Methods
    public static int GetIntElement(this Dictionary<string, string> Values, string Key)
    {
      return GetIntElement(Values, Key, -1);
    }

    public static int GetIntElement(this Dictionary<string, string> Values, string Key, int Default)
    {
      int ret = Default;

      var value = GetDictionaryElement(Values, Key);

      if(!string.IsNullOrEmpty(value))
        int.TryParse(value, out ret);

      return ret;

    }

    public static int GetIntElement(this Dictionary<string, string> Values, string Key, string Number)
    {
      return GetIntElement(Values, string.Format(Key, Number), -1);
    }

    public static double GetDoubleElement(this Dictionary<string, string> Values, string Key)
    {
      double ret = -1;

      var value = GetDictionaryElement(Values, Key);

      if(!string.IsNullOrEmpty(value))
        double.TryParse(value, out ret);

      return ret;

    }

    public static double GetDoubleElement(this Dictionary<string, string> Values, string Key, double Default)
    {
      double ret = Default;

      var value = GetDictionaryElement(Values, Key);

      if(!string.IsNullOrEmpty(value))
        double.TryParse(value, out ret);

      return ret;

    }

    public static double GetDoubleElement(this Dictionary<string, string> Values, string Key, string Number)
    {
      return GetDoubleElement(Values, string.Format(Key, Number), -1);
    }

    public static string GetDictionaryElement(this Dictionary<string, string> Values, string Key)
    {
      string ret = null;

      if(Values.ContainsKey(Key)) ret = Values[Key];

      return ret;
    }

    public static string GetDictionaryElement(this Dictionary<string, string> Values, string Key, string Number)
    {
      return GetDictionaryElement(Values, string.Format(Key, Number));
    }
    #endregion
  }
}
